using System;
using GameStateManagement.Input;
using Microsoft.Xna.Framework.Input.Touch;

namespace GameStateManagement.GravityWorld.Input
{
    class TrajectoryInputHandler : TouchInputHandler
    {
        private readonly GravityModel _game;

        public TrajectoryInputHandler(InputFsm controlerFsm, GravityModel game)
            : base(controlerFsm)
        {
            _game = game;
            _game.GameStateChanged += GameStateChanged;
        }

        protected override bool HandleTap(GestureSample gesture)
        {
            throw new NotImplementedException();
        }

        protected override bool HandleFreeDrag(GestureSample gesture)
        {
            LastFreeDragGesture = gesture;
            return _game.MoveFairy(gesture.Position);
        }

        protected override bool HandleDragComplete(GestureSample gesture)
        {
            return _game.LetGoOfFairy(LastFreeDragGesture.Position);
        }

        protected override bool HandlePinch(GestureSample gesture)
        {
            throw new NotImplementedException();
        }

        public override bool HandleInput(InputState state)
        {
            bool inputHandled = false;
            if (state.Gestures.Count > 0)
            {
                foreach (GestureSample gesture in state.Gestures)
                {
                    switch (gesture.GestureType)
                    {
                        case GestureType.FreeDrag:
                            inputHandled = HandleFreeDrag(gesture);
                            break;
                        case GestureType.DragComplete:
                            inputHandled = HandleDragComplete(gesture);
                            break;
                        default:
                            break;
                    }
                }
            }

            return inputHandled;
        }

        //Only one type of controler in this game so return.
        public void GameStateChanged(object sender, EventArgs e)
        {
            return;
        }
    }
}
